package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.RoleVo;
	import engine.tip.TipsMainLogic;
	import engine.ui.UIManage;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import newx.com.config.StaticIni;
	import newx.com.control.window.NxWindowEvent;
	
	/**
	 *跨服战身价
	 * @author yanghr
	 * 
	 */
	public class StridePerValueLogic
	{
		private static var _self:StridePerValueLogic;
		public static function getInstance():StridePerValueLogic
		{
			if(_self==null)
			{
				_self=new StridePerValueLogic();
			}
			return _self;
		}
		private var _ui:StridePerValueUI;
		public function get ui():StridePerValueUI 
		{
			if(_ui==null)
			{
				_ui=new StridePerValueUI;
				UIManage.getInstance().initUI(_ui,"StridePerValueUI");
			}
			return _ui;
		}
		private var _perValueArr:Array=[];
		
		public function clearData():void
		{
			_perValueArr=[];
		}
		/**
		 *添加人 
		 * @param value
		 * @param person
		 * 
		 */		
		public function addPerson(person:Object):void
		{
			for(var i:String in _perValueArr)
			{
				
				if(_perValueArr[int(i)][ActorPropertyUI.NAME] ==person[ActorPropertyUI.NAME])
				{
					_perValueArr[int(i)] =person;
					return;
				}
				
			}
			_perValueArr.push(person);
			
		}
		public function StridePerValueLogic()
		{
		}
		private var _bind:BindManage;
		private var _model:ModelLocator
		private var _role:RoleVo;
		private var _tip:TipsMainLogic;
		public function  Init():void
		{
			ui;
			_bind=BindManage.getInstance();
			_model=ModelLocator.getInstance();
			_role=_model.scene.Role;
			_tip=TipsMainLogic.instance;
		}
		/**
		 *排序 
		 * 
		 */		
		private function sortArr():void
		{
			_perValueArr.sort(function(item:Object,item2:Object):int{
				return item2[ActorPropertyUI.PER_VALUE]-item[ActorPropertyUI.PER_VALUE];
				
			});
			//			_perValueArr.reverse();
		}
		public function	BeforeShow():void
		{
			sortArr();
			var len:int=Math.min( _perValueArr.length,10)
			for (var i:int = 0; i <len;) 
			{
				var person:Object=_perValueArr[i];
				addInfo(++i,person[ActorPropertyUI.NAME],person[ActorPropertyUI.PER_VALUE]);
			}
			addAllEventListener()
		}
		public function	AfterHide():void
		{
			_ui.valueTable.dataProvider=null;
			removeAllEventListener()
		}
		public function addAllEventListener():void
		{
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,close);
			_ui.closeBtn.addEventListener(MouseEvent.CLICK,close);
		}
		public function removeAllEventListener():void
		{
			_ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,close)
			_ui.closeBtn.removeEventListener(MouseEvent.CLICK,close);
		}
		private function close(e:Event):void
		{
			_ui.hide();
		}
		/**
		 *吧数据添加到表格里 
		 * @param rank
		 * @param player
		 * @param value
		 * 
		 */		
		private function addInfo(rank:int,player:String,value:String):void
		{
			if(value=="0")return;
			var valuestr:String=StaticIni.getSwfLangStrVar("Stride_14",[value])
			_ui.valueTable.addLineByIndex([rank,player,valuestr],-1);
		}
	}
}